package stage1.operation.module4.chat;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

/**
 * 服务端，不知道为啥。断开链接的时候输出null
 */
public class ChatServer {
    public static ArrayList<Socket> socketList = new ArrayList<>();

    public static void main(String[] args) {
        ServerSocket serverSocket = null;

        try {
            serverSocket = new ServerSocket(8898);
            System.out.println("等待客户端连接...");
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (true) {
            Socket socket = null;
            while (true) {
                try {
                    socket = serverSocket.accept();
                    socketList.add(socket);
                    System.out.println("客户端 " + socket.getRemoteSocketAddress() + "连接成功！");

                    new Thread(new SendThreat(socket, socketList)).start();
                    new Thread(new ReceiveThreat(socket, socketList)).start();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
